<?php
/**
 * Copyright (c) 2008, Till Klampaeckel
 * 
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 * 
 *  * Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above copyright notice, this
 *    list of conditions and the following disclaimer in the documentation and/or
 *    other materials provided with the distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * PHP Version 5
 *
 * @category   Net
 * @package    Net_DNS2
 * @subpackage Net_DNS2_Header
 * @author     Eric Kilfoil <edk@ypass.net>
 * @author     Marco Kaiser <bate@php.net>
 * @author     Florian Anderiasch <fa@php.net>
 * @author     Till Klampaeckel <till@php.net>
 * @copyright  1997-2008 The respective owners.
 * @license    http://www.opensource.org/licenses/bsd-license.php The BSD License
 * @version    Release: @package_version@
 * @link       http://net-dns2.googlecode.com/
 */

/**
 * Net_DNS2_Abstract
 */
require_once 'Net/DNS2/Abstract.php';

/**
 * Builds or parses the QUESTION section of a DNS packet
 *
 * Builds or parses the QUESTION section of a DNS packet
 *
 * @category   Net
 * @package    Net_DNS2
 * @subpackage Net_DNS2_Question
 * @author     Eric Kilfoil <edk@ypass.net>
 * @author     Marco Kaiser <bate@php.net>
 * @author     Florian Anderiasch <fa@php.net>
 * @author     Till Klampaeckel <till@php.net>
 * @copyright  1997-2008 The respective owners.
 * @license    http://www.opensource.org/licenses/bsd-license.php The BSD License
 * @version    Release: @package_version@
 * @link       http://net-dns2.googlecode.com/
 */
class Net_DNS2_Question extends Net_DNS2_Abstract
{
    var $qname = null;
    var $qtype = null;
    var $qclass = null;

    /**
     * @param string $qname
     * @param string $qtype
     * @param string $qclass
     * 
     * @return void
     */
    public function __construct($qname, $qtype, $qclass)
    {
        $qtype  = !is_null($qtype)  ? strtoupper($qtype)  : 'ANY';
        $qclass = !is_null($qclass) ? strtoupper($qclass) : 'ANY';

        // Check if the caller has the type and class reversed.
        // We are not that kind for unknown types.... :-)
        if ((is_null(Net_DNS2::typesbyname($qtype))
            || is_null(Net_DNS2::classesbyname($qtype)))
            && !is_null(Net_DNS2::classesbyname($qclass))
            && !is_null(Net_DNS2::typesbyname($qclass))) {

            list($qtype, $qclass) = array($qclass, $qtype);
        }
        
        $qname = preg_replace(array('/^\.+/', '/\.+$/'), '', $qname);
        
        $this->qname  = $qname;
        $this->qtype  = $qtype;
        $this->qclass = $qclass;
    }

    /**
     * @return string
     */
    public function string()
    {
        return $this->qname . ".\t" . $this->qclass . "\t" . $this->qtype;
    }
    
    /**
     * @param string $packet
     * @param int    $offset
     * 
     * @return string
     */
    public function data($packet, $offset)
    {
        $data  = $packet->dn_comp($this->qname, $offset);
        $data .= pack('n', Net_DNS2::typesbyname(strtoupper($this->qtype)));
        $data .= pack('n', Net_DNS2::classesbyname(strtoupper($this->qclass)));
        
        return $data;
    }
}
?>